package rmi;


import gui.mainWindow;

import java.awt.GridLayout;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.util.List;

import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import document.DocPermissions;
import document.Document;
import messagepasser.UserInfo;

public class Client_Servant extends UnicastRemoteObject implements Client {

	mainWindow mainWindow;
	int userid;

	public Client_Servant(mainWindow i, int userid) throws RemoteException {
		mainWindow = i;
		this.userid = userid;
	}

	@Override
	public void newDocument(int docid, String docName) throws RemoteException {

		Document d = null;
		DocPermissions[] perDocusers;

		System.out.println ("new user");
		Object[] OkCancel = { "Ok", "Cancel" };
		JLabel jl = new JLabel("New Document available for sharing");
		JOptionPane.showOptionDialog(null, jl, "Permissions Changed", JOptionPane.DEFAULT_OPTION, JOptionPane.NO_OPTION, null, OkCancel, OkCancel[0]);


		try {
			perDocusers = mainWindow.tecBoxServer.getDocUsers(docid);
			String content  = mainWindow.tecBoxServer.editdoc(docid);
			if (content == null) {
				content = " ";
			}
			d = new Document(docid, mainWindow.me, mainWindow.users, perDocusers, docName, content);
			mainWindow.docNames.add(docName);
			mainWindow.docs = mainWindow.tecBoxServer.getDocuments(userid);
			mainWindow.userDocs.add(d);
			mainWindow.getDocList().setListData(mainWindow.docNames);
		}
		catch (Exception e) {

		}
		mainWindow.createPage (d);
		mainWindow.getjTabbedPane1().addTab( d.getDocName(), d.panel );

	}

	@Override
	public void newUserOnline(UserInfo u) throws RemoteException {

		boolean newRegUser = true; 

		for (UserInfo user : mainWindow.users) {
			if (user.getUserid() == u.getUserid()) {
				user.setIpaddr(u.getIpaddr());
				user.setOnlinestatus(u.isOnlinestatus());
				user.setPort(u.getPort());
				newRegUser = false;
				for (Document doc : mainWindow.userDocs) {
					doc.getMyPasser().updateUserInfo(u);
				}
				break;
			}
		}

		if (newRegUser) {
			mainWindow.users.add(u);
		}

		if (mainWindow.userNames.contains(u.getUsername()) == false) {
			mainWindow.userNames.add(u.getUsername());
		}
		mainWindow.getOnlineUsers().setListData(mainWindow.userNames);

	}	

	public void userOffline(int userId) throws RemoteException {
		System.out.println ("logout called");

		for (UserInfo user : mainWindow.users) {
			if (user.getUserid() == userId) {
				user.setIpaddr(null);
				user.setOnlinestatus(false);
				user.setPort(0);
				mainWindow.userNames.remove(user.getUsername());
				break;
			}
		}

		mainWindow.getOnlineUsers().setListData(mainWindow.userNames);
	}

	@Override
	public int getUserId() throws RemoteException {
		// TODO Auto-generated method stub
		return userid;
	}

	@Override
	public void newDocUser(int docid, UserInfo u) throws RemoteException {

		// TODO Auto-generated method stub
		mainWindow.getDocumentFromId(docid).getUsers().add(u);
		mainWindow.getDocumentFromId(docid).getMyPasser().addNewUser(u);

	}	
	@Override
	public void acceptRequest(UserInfo dest)
	throws RemoteException {
		mainWindow.startTxtChat(dest);

	}
	
	public void startVoiceChatReceiver(UserInfo dest)
	throws RemoteException {
		System.out.println("yes");
		mainWindow.startVoiceChatReceiver(dest);
	}
	
	public void startVoiceChatTx(UserInfo dest)
	throws RemoteException {
		mainWindow.addVoiceChatUsers(dest.getUsername());
		mainWindow.startVoiceChatTx(dest);
	}
	public Boolean getPermForVoiceChat(UserInfo dest)throws RemoteException{
		System.out.println("getPermForVoiceChat Called");
		Object[] OkCancel = {"OK", "Cancel" };
		String name = dest.getUsername();
		int a = JOptionPane.showOptionDialog(null, "Do you want to Voice Chat with "+name + " ?", "Current Permissions", JOptionPane.DEFAULT_OPTION, JOptionPane.NO_OPTION, null, OkCancel, OkCancel[0]);
		if(a==0)
			return true;
		else
			return false;
	}

}
